package org.steven.crm.app.api.processor;

import org.steven.crm.app.api.LocalDataCache;
import org.steven.crm.app.api.processor.base.BaseProcessor;
import com.steven.common.utils.StringUtil;
import com.steven.validate.StringValidator;
import com.steven.xweb.web.CheckList;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author wuzw
 * @Date 2023/5/20 20:05
 * @Version 1.0
 */
public class DictProcessor extends BaseProcessor {

    public boolean getDictValuesActionCheck() throws Exception {
        CheckList checkList = getChecklist();
        checkList.addCheckItem(new StringValidator("dictCode", "字典编码", getStringData("dictCode"), true));
        return checkList.check();
    }

    public void getDictValuesAction() throws Exception {
        String dictCode = getStringData("dictCode");
        List<Map<String, String>> datas = LocalDataCache.getInstance().getDictDatas(dictCode);
        setObjectData(datas);
    }

    public void batchGetDictValuesAction() throws Exception {
        Map<String, List<Map<String, String>>> result = new HashMap<>();

        String dictCodes = getStringData("dictCodes");
        String[] dictCodeArrs = StringUtil.split(dictCodes.replaceAll(" ", ""), ",");

        for (String dictCode : dictCodeArrs) {
            List<Map<String, String>> datas = LocalDataCache.getInstance().getDictDatas(dictCode);
            result.put(dictCode, datas);
        }
        setObjectData(result);
    }
}
